package com.txc.books.service.impl;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.txc.books.entity.User;
import com.txc.books.mapper.UserMapper;
import com.txc.books.service.IUserService;

import com.txc.books.utils.PageRequest;
import com.txc.books.utils.PageResult;
import com.txc.books.utils.PageUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;


import java.util.List;


/**
 * @author: XCTANG
 *
 */

@Service
public class UserServiceImpl implements IUserService {

    private UserMapper userMapper;

    public UserServiceImpl(UserMapper userMapper){
        this.userMapper = userMapper;
    }

    @Override
    public List<User> findAll() {
        return userMapper.findAll();
    }

    @Override
    public void insert(User user) {

        String name = user.getName();
        if (StringUtils.isEmpty(name)){
            throw new IllegalArgumentException("名字不能为空");
        }
        if (StringUtils.isBlank(name)){
            throw new IllegalArgumentException("名字不能为空白");
        }
        userMapper.insert(user);
    }

    @Override
    public void update(User user) {

        String name = user.getName();
        if (StringUtils.isEmpty(name)){
            throw new IllegalArgumentException("名字不能为空");
        }
        if (StringUtils.isBlank(name)){
            throw new IllegalArgumentException("名字不能为空白");
        }userMapper.update(user);
    }

    @Override
    public void delete(Integer id) {
        userMapper.delete(id);
    }

    @Override
    public void select(Integer id) {
        userMapper.select(id);
    }


    @Override
    public PageResult findByPager(PageRequest pageRequest) {
        return PageUtils.getPageResult(pageRequest, getPageInfo(pageRequest));
    }

    public PageInfo<User> getPageInfo(PageRequest pageRequest) {
        int pageNum = pageRequest.getPageNum();
        int pageSize = pageRequest.getPageSize();
        PageHelper.startPage(pageNum, pageSize);
        List<User> userMenus = userMapper.selectPage();
        return new PageInfo<User>(userMenus);
    }
}
